<?php
if (!(defined('IN_IA')))
{
    exit('Access Denied');
}
/**
 * User: 邓志锋 <280160522@qq.com> <http://www.diygw.com>
 * Date: 2018-07-28
 * Time: 下午 6:06
 */
class Order_Diygwcom extends DiygwcomAction
{
    public function index()
    {
        global $_GPC, $_W;
        $dashboardid=$_GPC['dashboardid'];
        $_SESSION['dashboardid']=$dashboardid;
        include $this->template();
    }

    public function data()
    {
        global $_GPC, $_W;
        $dashboardid = $_SESSION['dashboardid'];
        $order['dashboard_id']=$dashboardid;

        $psize 	= 10;
        if( isset($_GPC['r'])){
            $psize = safe_gpc_int($_GPC['r']);
        }else if( isset($_GPC['row'])){
            $psize = safe_gpc_int($_GPC['row']);
        }
        $index 	= max(1, intval($_GPC['page']));
        $plist 	= pdo_getslice(getTableName("order"), $order , array($index, $psize), $total, array(), '',['id desc']);
        $rows=[];
        foreach ($plist as $item){
            switch ($item['status']){
                case "1":
                    $item['status']="订单待支付";
                    break;
                case "2":
                    $item['status']="订单已付款待发货";
                    break;
                case "2":
                    $item['status']="订单待收货";
                    break;
                case "4":
                    $item['status']="交易完成";
                case "5":
                    $item['status']="订单已退款";
                    break;
            }
            $rows[]=$item;
        }
        $list['rows']=$rows;
        $list['total']=$total;
        $list['totalPage']=ceil($total / $psize);
        $list['message']="获取数据成功";
        echo json_encode($list);

    }

    public function refunddata()
    {
        try {
            global $_GPC, $_W;
            $dashboardid = $_SESSION['dashboardid'];
            $ids    =   safe_gpc_array($_GPC['values']);
            $map['order_id'] =$ids;
            $map['status'] ="2";
            $orders = pdo_getall(getTableName("order_pay"),$map);
            foreach ($orders as $item){
                load()->model('refund');
                $refund_id = refund_create_order($item['trade_no'], MODULE_NAME, $item['pay_price'], "无理由退款");
                if (is_error($refund_id)) {
                    message($refund_id['message'], '', 'error');
                }
                $refund_id = refund($refund_id);
                if(isset($refund_id['errno'])){
                    message($refund_id['message'], '', 'error');
                }else{
                    $order = array(
                        'status'	=>5
                    );
                    $result = pdo_update(getTableName('order'), $order, array('id' =>$item['order_id']));
                    $result = pdo_update(getTableName('order_pay'), $order, array('id' =>$item['id']));
                    if(false!==$result){
                        // 发送文本消息提醒
                        $text = '您的订单：'.$item['title']."\n订单号：".$item['trade_no']."\n";
                        $custom = array(
                            'msgtype' => 'text',
                            'text' => array('content' => urlencode($text.'退款成功')),
                            'touser' => $order['openid'],
                        );
                        $account_api = WeAccount::create();
                        $result = $account_api->sendCustomNotice($custom);
                        message('退款成功', '', 'success');
                    }else {
                        message('退款失败！', '', 'error');
                    }
                }
            }
            global $_GPC;
            $reason = safe_gpc_string($_GPC['reason']);
            $order_info = pdo_get(getTableName('order'), array('id' => safe_gpc_int($_GPC['oid'])), array('ordersn', 'price', 'state', 'status'));
            if($order_info['state']==0){
                message('订单未支付不能发起退款！', '', 'error');
            }

            if(pdo_delete(getTableName("order"),$map)){
                $info = ['status'=>'success', 'message'=>'退款成功'];
                echo json_encode($info);
            }else{
                $info = ['status'=>'error', 'message'=>'退款失败'];
                echo json_encode($info);
            }
        } catch (\Exception $e) {
            $info = ['status'=>'error', 'message'=>'退款失败'];
            echo json_encode($info);
        }
    }


    public function remove()
    {
        try {
            global $_GPC, $_W;
            $ids    =   safe_gpc_array($_GPC['values']);
            $dashboardid = $_SESSION['dashboardid'];
            $map['dashboard_id'] =$dashboardid;
            $map['id'] =$ids;

            pdo_delete(getTableName("order"),$map);
            $mapp['order_id'] =$ids;
            pdo_delete(getTableName("order_pay"),$mapp);
            $info = ['status'=>'success', 'message'=>'删除数据成功'];
            echo json_encode($info);
        } catch (\Exception $e) {
            $info = ['status'=>'error', 'message'=>'删除数据失败'];
            echo json_encode($info);
        }
    }
}